package com.ly.heart.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ly.common.core.domain.AjaxResult;
import com.ly.common.exception.CustomException;
import com.ly.common.utils.DateUtils;
import com.ly.common.utils.SecurityUtils;
import com.ly.common.utils.erp.ConstUtil;
import com.ly.common.utils.erp.SnowflakeIdWorker;
import com.ly.heart.mapper.KingBaseSafetyStockMapper;
import com.ly.heart.service.IKingBaseSafetyStockService;
import com.ly.heart.util.CurrentUserUtil;
import org.springframework.stereotype.Service;
import com.ly.heart.domain.KingBaseSafetyStockEntity;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 金蝶-财务模块-安全库存设置Service业务层处理
 * @author wutao
 * @date 2024-06-22
 */
@Service
public class KingBaseSafetyStockServiceImpl extends ServiceImpl<KingBaseSafetyStockMapper, KingBaseSafetyStockEntity> implements IKingBaseSafetyStockService {

    /**
     * 查询金蝶-财务模块-安全库存设置
     * @param id 金蝶-财务模块-安全库存设置ID
     * @return 金蝶-财务模块-安全库存设置
     */
    @Override
    public KingBaseSafetyStockEntity selectKingBaseSafetyStockEntityById (String id) {
        return this.getById ( id );
    }

    /**
     * 查询金蝶-财务模块-安全库存设置列表
     * @param kingBaseSafetyStockEntity 金蝶-财务模块-安全库存设置
     * @return 金蝶-财务模块-安全库存设置
     */
    @Override
    public AjaxResult selectKingBaseSafetyStockEntityList (KingBaseSafetyStockEntity kingBaseSafetyStockEntity) {
        IPage page = this.page ( new Page<> ( kingBaseSafetyStockEntity.getPage (), kingBaseSafetyStockEntity.getLimit () ), new LambdaQueryWrapper<KingBaseSafetyStockEntity> ().eq ( KingBaseSafetyStockEntity :: getIsUsed, ConstUtil.IS_USED_YES ) );
        return AjaxResult.success ( page );
    }

    /**
     * 新增金蝶-财务模块-安全库存设置
     * @param kingBaseSafetyStockEntity 金蝶-财务模块-安全库存设置
     * @return 结果
     */
    @Override
    public AjaxResult insertKingBaseSafetyStockEntity (KingBaseSafetyStockEntity kingBaseSafetyStockEntity) {
        kingBaseSafetyStockEntity.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
        kingBaseSafetyStockEntity.setDeptId ( CurrentUserUtil.getCompanyId () );
        kingBaseSafetyStockEntity.setCreateBy ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
        kingBaseSafetyStockEntity.setCreateTime ( DateUtils.getNowDate () );
        boolean save = this.save ( kingBaseSafetyStockEntity );
        if ( ! save ) {
            throw new CustomException ( "新增安全库存失败！" );
        }
        return AjaxResult.success ();
    }

    /**
     * 修改金蝶-财务模块-安全库存设置
     * @param kingBaseSafetyStockEntity 金蝶-财务模块-安全库存设置
     * @return 结果
     */
    @Override
    public AjaxResult updateKingBaseSafetyStockEntity (KingBaseSafetyStockEntity kingBaseSafetyStockEntity) {
        kingBaseSafetyStockEntity.setUpdateBy ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
        kingBaseSafetyStockEntity.setUpdateTime ( DateUtils.getNowDate () );
        boolean b = this.updateById ( kingBaseSafetyStockEntity );
        if ( ! b ) {
            throw new CustomException ( "修改安全库存失败！" );
        }
        return AjaxResult.success ();
    }

    /**
     * 批量删除金蝶-财务模块-安全库存设置
     * @param ids 需要删除的金蝶-财务模块-安全库存设置ID
     * @return 结果
     */
    @Override
    public AjaxResult deleteKingBaseSafetyStockEntityByIds (String[] ids) {
        List<KingBaseSafetyStockEntity> list = this.list (new LambdaQueryWrapper<KingBaseSafetyStockEntity> ().in ( KingBaseSafetyStockEntity::getId, ids ));
        List<String> idList = list.stream ().filter ( stock -> stock.getIsDel () == 1 ).map ( KingBaseSafetyStockEntity :: getId ).collect ( Collectors.toList () );
        StringBuilder stringBuilder = new StringBuilder ( "【" );
        list.forEach ( stock -> {
            if ( stock.getIsDel () == 2 ) {
                stringBuilder.append ( stock.getMaterialCode () );
            }
        } );
        stringBuilder.append ( "】" );
        boolean update = this.update ( new LambdaUpdateWrapper<KingBaseSafetyStockEntity> ().
                set ( KingBaseSafetyStockEntity :: getIsUsed, ConstUtil.IS_USED_NO ).
                set ( KingBaseSafetyStockEntity :: getUpdateBy, SecurityUtils.getLoginUser ().getUser ().getUserId () ).
                set ( KingBaseSafetyStockEntity :: getUpdateTime, DateUtils.getNowDate () ).
                in ( KingBaseSafetyStockEntity :: getId, idList ) );
        if ( ! update ) {
            throw new CustomException ( "删除安全库存失败！" );
        }
        String str = "删除安全库存成功";
        if ( list.size () > 0 ) {
            str.concat ( "，其中" ).concat ( stringBuilder.toString () ).concat ( "被引用，不可删除" );
        }
        return AjaxResult.success ( str );
    }

    /**
     * 删除金蝶-财务模块-安全库存设置信息
     * @param id 金蝶-财务模块-安全库存设置ID
     * @return 结果
     */
    @Override
    public AjaxResult disabled (String id) {
        KingBaseSafetyStockEntity safetyStockEntity = this.getById ( id );
        if ( safetyStockEntity == null ) {
            throw new CustomException ( "无此安全库存！" );
        }
        boolean update = this.update ( new LambdaUpdateWrapper<KingBaseSafetyStockEntity> ().
                set ( KingBaseSafetyStockEntity :: getIsDisabled, safetyStockEntity.getIsDisabled () == 1 ? 2 : 1 ).
                set ( KingBaseSafetyStockEntity :: getUpdateBy, SecurityUtils.getLoginUser ().getUser ().getUserId () ).
                set ( KingBaseSafetyStockEntity :: getUpdateTime, DateUtils.getNowDate () ).
                eq ( KingBaseSafetyStockEntity :: getId, id ) );
        if ( ! update ) {
            throw new CustomException ( safetyStockEntity.getIsDisabled () == 1 ? "禁用安全库存失败！" : "解禁安全库存失败！" );
        }
        return AjaxResult.success ();
    }

}
